package com.train.common.utils.captcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.train.common.utils.spring.SpringUtils;
import io.micrometer.common.util.StringUtils;

import java.awt.image.BufferedImage;

/**
 * 图形验证码工具类
 */
public class CaptchaUtils {

    public static String createDefaultText() {
        DefaultKaptcha kaptchaBean = SpringUtils.getBean("captchaProducer", DefaultKaptcha.class);
        return kaptchaBean.createText();
    }

    public static BufferedImage createCaptcha(String text) {
        if (StringUtils.isEmpty(text)) {
            text = createDefaultText();
        }
        DefaultKaptcha kaptchaBean = SpringUtils.getBean("captchaProducer", DefaultKaptcha.class);
        return kaptchaBean.createImage(text);
    }
}
